(function ($) {

    var ERROR_MSG = {
        '1':'输入参数错误 ARGS_ERR',                   //输入参数错误
        '2':'参考边错误 REFER_BORDER_ERR',            //参考边错误
        '4':'航点个数过多 TOO_MANY_WAYPOINT_ERR',      //航点个数过多
        '8':'地块边界点过近 BORDER_POINT_TOO_CLOSE',      //地块边界点过近
        '16':'航线在地块边界外 ROUTELINE_OUT_OF_BORDER',  //航线在地块边界外
        '32':'Waypoint距离过近 WAYPOINT_TOO_CLOSE',         //Waypoint距离过近
        '64':'base border点太近 BASE_BORDER_POINT_TOO_CLOSE', //base border点太近
        '128':'转弯设置错误 SEGMENT_ATTR_ERR'          //转弯设置错误
    };

    /*
    var ERROR_MSG = {
        '1': '输入参数错误',
        '2': '参考边错误',
        '4': '航点个数过多',
        '8': '地块边界点过近',
        '16': '航线在地块边界外',
        '32': 'Waypoint距离过近',
        '64': 'base border点太近',
        '128': '转弯设置错误',
        '256': 'Waypoint距离过远',
        '512': 'FIRST Waypoint距离过远',
        '1024': 'LAST Waypoint距离过远',
        '-100000': '调C程序错误',
        '-100001': '地块有边界长度小于1米',
        '-100002': '障碍物有边界长度小于1米',
        '-100003': '漏喷超过允许的范围'
    };
    */

    var html = {
        thead_query: function () {
            return [
                '<tr>',
                '    <th>任务号</th>',
                '    <th>地块名称</th>',
                '    <th>地块面积</th>',
                '    <th>错误数量</th>',
                '    <th>操作</th>',
                '</tr>'
            ].join('');
        },
        tr_query: function (data) {
            var size = data.problem_list && Object.keys(data.problem_list).length || 0;
            var _html = [
                '<tr>',
                '   <td>', data.task_code, '</td>',
                '   <td>', data.field_name, '</td>',
                '   <td>', data.field_area, '</td>',
                '   <td>', size, '</td>',
                '   <td>',
                '       <a style="cursor: pointer;" data-toggle="modal" data-target="#myModal">查看</a>',
                '   </td>',
                '</tr>'
            ].join('');
            return _html;
        },
        thead_leakage: function () {
            return [
                '<tr>',
                '    <th>任务号</th>',
                '    <th>地块名称</th>',
                '    <th>地块面积</th>',
                '    <th>参考边</th>',
                '    <th >',
                '       <div style="display: flex;line-height: 7px;">漏喷面积',
                '           <div class="self-arrow-up" data-field="leakage_area" data-sort="ASC"></div>  ',
                '           <div class="self-arrow-up self-arrow-down" data-field="leakage_area" data-sort="DESC"></div>',
                '       </div>',
                '    </th>',
                '    <th >',
                '       <div style="display: flex;line-height: 7px;">漏喷率',
                '           <div class="self-arrow-up" data-field="leakage" data-sort="ASC"></div>  ',
                '           <div class="self-arrow-up self-arrow-down" data-field="leakage" data-sort="DESC"></div>',
                '       </div>',
                '    </th>',
                '    <th>操作</th>',
                '</tr>'
            ].join('');
        },
        tr_leakage: function (data) {
            var _html = [
                '<tr>',
                '   <td>', data.task_code, '</td>',
                '   <td>', data.field_name, '</td>',
                '   <td>', data.field_area, '</td>',
                '   <td>', data.calc_ref_border, '</td>',
                '   <td>', data.leakage_area, '</td>',
                '   <td>', data.leakage, '</td>',
                '   <td>',
                '       <a style="cursor: pointer;" data-toggle="modal" data-target="#myModal">查看</a>',
                '   </td>',
                '</tr>'
            ].join('');
            return _html;
        },
        no_record: function (btn) {
            if (btn == 'leakage') {
                return '<tr><td colspan="7" align="center">没有错误记录</td></tr>';
            }
            return '<tr><td colspan="5" align="center">没有错误记录</td></tr>';
        },
        li: function (isActive, text) {
            return [
                (isActive ? '<li class="active">' : '<li>'),
                '   <a href="#tab1" data-toggle="tab">', text, '</a>',
                '</li>'
            ].join('');
        },
        mark: function (opts) {
            var currentLabel = opts.currentLabel,
                problem_list = opts.problem_list,
                fly_params = opts.fly_params || {};

            if (!currentLabel) {
                currentLabel = Object.keys(problem_list)[0];
            }

            var error = problem_list[currentLabel];

            return [
                '<div>',
                '   <div>',
                '       <span>喷幅: </span>',
                '       <span>', error.d || 0, '</span>',
                '   </div>',
                '   <div>',
                '       <span>错误码: </span>',
                '       <span>', error.error_code || '', '</span>',
                '   </div>',
                '   <div>',
                '       <span>错误描述: </span>',
                '       <span>', ERROR_MSG[error.error_code] || '漏喷', '</span>',
                '   </div>',
                '   <div>',
                '       <span>地块面积: </span>',
                '       <span>', error.field_area_py || 0, '平方米</span>',
                '   </div>',
                '   <div>',
                '       <span>有效喷洒面积: </span>',
                '       <span>', error.spray_area || 0, '平方米</span>',
                '   </div>',
                '   <div>',
                '       <span>漏喷面积: </span>',
                '       <span>', error.leakage_area || 0, '平方米</span>',
                '   </div>',
                '   <div>',
                '       <span>漏喷率: </span>',
                '       <span>', error.leakage || 0, '</span>',
                '   </div>',
                '   <div>',
                '       <span>距离: </span>',
                '       <span>', error.total_fly_distance || 0, '米</span>',
                '   </div>',
                '</div>'
            ].join('');

            /*
            var leakage = error && error.leakage_calculation || {};

            var _code = error.error_code || -1, _msg = ERROR_MSG[_code] || '未知错误';
            return [
                '<div>',
                '   <div>',
                '       <span>喷幅: </span>',
                '       <span>', (fly_params['gap']) || '未知', '</span>',
                '   </div>',
                '   <div>',
                '       <span>错误码: </span>',
                '       <span>', _code, '</span>',
                '   </div>',
                '   <div>',
                '       <span>错误描述: </span>',
                '       <span>', _msg, '</span>',
                '   </div>',
                '   <div>',
                '       <span>地块面积: </span>',
                '       <span>', opts.field_area || 0, '</span>',
                '   </div>',
                '   <div>',
                '       <span>有效喷洒面积: </span>',
                '       <span>', leakage.vaild_pray || 0, '</span>',
                '   </div>',
                '   <div>',
                '       <span>漏喷率: </span>',
                '       <span>', (leakage.leakage) || 0, '</span>',
                '   </div>',
                '</div>'
            ].join('');
            */
        }

    };
    var handler = {
        a: function (opts) {
            var _line_data = opts.data;
            var _title_container = $('#myModalLabel'), _tabs_container = $('.nav-tabs'), _show_container = $('#show');

            _title_container.text('任务号：' + _line_data.task_code);

            var map = {};
            window.setTimeout(function (args) {
                map = $.uavMap.init();
                $.uavMap.render(map, _line_data);

                _show_container.html(html.mark(_line_data));
            }, 500);

            _tabs_container.html('');
            $.each(Object.keys(_line_data.problem_list), function (i, json) {
                var isActive = _line_data.calc_ref_border ? _line_data.calc_ref_border == json : i == 0;
                var _li = $(html.li(isActive, json));

                _li.on('click', function () {
                    var _current = $(this).find('a').text();
                    _line_data.currentLabel = _current;

                    $.uavMap.switchTab(map, _line_data);

                    _show_container.html(html.mark(_line_data));
                });
                _tabs_container.append(_li);
            });
        },
        tr: function (opts) {
            var tr = opts.tr, _data = opts.data;
            tr.find('a').on('click', function () {
                handler.a({data: _data});
            });
        }
    }

    $.task = {
        tip: function (tipId, _global) {
            var _tip = $('#' + tipId);
            _tip.find('.batch-no').text(_global.batch_no || '');
            ;
            _tip.find('.all-task-size').text(_global.allNum || 0);
            _tip.find('.current-task-size').text(_global.currentHandleNum || 0);
            // _tip.find('.error-task-size').text(_global.errorNum || 0);
            _tip.find('.base-error-task-size').text(_global.baseErrorNum || 0);
            _tip.find('.leakage-task-size').text(_global.leakageNum || 0);
            _tip.show();
        },

        render: function (opts) {
            var _container = $('#' + opts.containerId),
                _list = opts.resultList,
                _btn = opts.btn;

            var _tbody = _container.find('tbody').html('');

            _container.find('thead').html(html['thead_' + _btn]());

            var len = _list.length;
            if (len <= 0) {
                _tbody.html(html.no_record(_btn));
                return;
            }

            for (var i = 0; i < _list.length; i++) {
                var _data = _list[i];
                _data.problem_list = JSON.parse(_data.problem_list);
                _data.fly_params = JSON.parse(_data.fly_params);

                var tr = $(html['tr_' + _btn](_data));

                handler.tr({tr: tr, data: _data})

                _tbody.append(tr);
            }

        }
    };

})(jQuery);
